package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ExperienceRuleService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.model.ExperienceRuleModel;

/**
 * 体验券规则管理Aciton
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 下午5:52:19
 */
public class ManageExperienceRuleAction extends BaseAction<ExperienceRuleModel> implements ModelDriven<ExperienceRuleModel> {
	
	@Resource
	private ExperienceRuleService experienceRuleService;
	
	/**
	 * 体验券规则实体对象
	 */
	private ExperienceRule experienceRule;
	
	private Map<String, Object> data;
	
	@Action("/modules/additional/experienceRule/manage")
	public String manage(){
		return "manage";
	}
	
	/**
	 * 获取列表信息
	 * @throws IOException
	 */
	@Action("/modules/additional/experienceRule/listJson")
	public void listJson() throws IOException{
		data = new HashMap<String, Object>();
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus((byte)99);
		}
		PageDataList<ExperienceRule> pagaDataList = experienceRuleService.findByModel(model);
		int total = pagaDataList.getPage().getTotal();
		data.put("total", total);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 添加
	 * @return
	 */
	@Action("/modules/additional/experienceRule/append")
	public String append(){
		return "append";
	}
	
	/**
	 * 添加
	 * @throws Exception
	 */
	@Action("/modules/additional/experienceRule/doAppend")
	public void doAppend() throws Exception {
		data = new HashMap<String, Object>();
		model.validModelData();
		ExperienceRule experienceRule = model.prototypeForAppend();
		experienceRuleService.save(experienceRule);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102004001"));
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/additional/experienceRule/modify")
	public String modify() {
		ExperienceRule experienceRule = experienceRuleService.findById(model.getId());
		request.setAttribute("experienceRule", experienceRule);
		return "modify";
	}
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/additional/experienceRule/doModify")
	public void doModify() throws Exception {
		data = new HashMap<String, Object>();
		model.validModelData();
		ExperienceRule experienceRule = model.prototype();
		experienceRuleService.update(experienceRule);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102004001"));
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 禁用
	 * @return
	 */
	@Action("/modules/additional/experienceRule/disable")
	public void disable() throws Exception {
		int resule = experienceRuleService.updateStatus(model.getId(), ExperienceRuleModel.EXPERIENCERULE_STATUS_DISABLE, ExperienceRuleModel.EXPERIENCERULE_STATUS_ENABLE);
		if(resule == 1){
			printWebSuccess();
		} else {
			printWebResult(MessageUtil.getMessage("MF102004001"), false);
		}
	}
	
	/**
	 * 启用
	 * @return
	 */
	@Action("/modules/additional/experienceRule/enable")
	public void enable() throws Exception {
		int resule = experienceRuleService.updateStatus(model.getId(), ExperienceRuleModel.EXPERIENCERULE_STATUS_ENABLE, ExperienceRuleModel.EXPERIENCERULE_STATUS_DISABLE);
		if(resule == 1){
			printWebSuccess();
		} else {
			printWebResult(MessageUtil.getMessage("MF102004001"), false);
		}
	}

	@Action("/modules/additional/experienceRule/experienceRuleJson")
	public void experienceRuleListJson() throws IOException {
		List<ExperienceRule> list = experienceRuleService.findUsableExperienceRule();
		JSONArray jsonArray = new JSONArray();//new一个json数组
		if (!list.isEmpty() && list.size() > 0) {
			for (ExperienceRule entity : list) {
				JSONObject obj = new JSONObject();
				obj.put("ruleId", entity.getId());
				obj.put("ruleName", entity.getName());
				jsonArray.add(obj);//循环new jsonObject
			}
		}
		printJson(jsonArray.toString());
	}

	
	public ExperienceRule getExperienceRule() {
		return experienceRule;
	}

	public void setExperienceRule(ExperienceRule experienceRule) {
		this.experienceRule = experienceRule;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}
	
}
